﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_Company_manage : System.Web.UI.Page
{
    public int projectid;
    protected void Page_Load(object sender, EventArgs e)
    {


        projectid = SysFun.ToInt(Request.QueryString["projectid"]);
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (projectid != 0)
            {
                Init1(projectid);
            }
        }

    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        //自动编号
        string sql;
        Db db = new Db();
        sql = "update ProjectCompanyAttribution set companynum=id where companynum is null";
        db.Command(sql);
        db.Close();
        Init1(projectid);
    }
    private void Init1(int projectid)
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        string strGetPageLink = "";
        dbPage.Table = "ProjectCompanyAttribution inner join Company on ProjectCompanyAttribution.AccountId=Company.AccountId ";                                               //表名，可以多个表关联
        dbPage.Key = "ProjectCompanyAttribution.Id";
        string sql = "";
        //企业名称
        if (ComPanyName.Text.Trim().Length > 0)
        {
            sql = "CompanyName Like '%" + ComPanyName.Text.Trim() + "%' and ";
            strGetPageLink = "&CompanyName=" + Server.UrlEncode(ComPanyName.Text.Trim());
        }
        //企业注册号
        if (usercode.Text.Trim().Length > 0)
        {
            sql = "usercode Like '%" + usercode.Text.Trim() + "%' and ";
            strGetPageLink = "&usercode=" + Server.UrlEncode(usercode.Text.Trim());
        }

        dbPage.Where = sql + " ProjectCompanyAttribution.projectid=" + projectid + "  ";
        dbPage.SelectField = "ProjectCompanyAttribution.Id as id1,CompanyName,TrialHabitus,CompanyNum,usercode,ProjectCompanyAttribution.AccountId as AccountId_TB ";                        //可以不设，默认为“*”
        dbPage.OrderBy = "CompanyName";                                //排序字段，可以不输入。不输入时以 Key 作为排序字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]); //设置需要查询第几页
        dbPage.PageSize = 20;                                                   //设置每页显示多少条记录

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("<table width='100%' cellspacing='0' border='1' cellpadding='3'>");
        sb.Append("<tr>");
        sb.Append("<td class='table_title' nowrap>投标人</td>");
        sb.Append("<td class='table_title' nowrap>投标人注册号</td>");//添加此项以方便用户查询
        sb.Append("<td class='table_title' nowrap>添加生产企业</td>");
        sb.Append("<td class='table_title' nowrap>删除</td>");
        sb.Append("<td class='table_title' nowrap>生产企业</td>");

        sb.Append("</tr>");

        int intNum = 0, j = 0;
        string _strSql = "";
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        Db db2 = new Db();
        System.Data.DataTable dt;
        System.Text.StringBuilder pcaID = new System.Text.StringBuilder();
        while (dr.Read())
        {
            intNum++;


            if (j == 0)
            {
                sb.Append("<tr class='table_td'>");
                j++;
            }
            else
            {
                sb.Append("<tr class='table_td2'>");
                j--;
            }

            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["CompanyName"]) + "</td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["usercode"]) + "</td>");
           
            sb.Append("<td><a target=\"_blank\" href=\"Company_SC_Select.aspx?AccountId_TB=" + SysFun.ToTrim(dr["AccountId_TB"]) + "&ProjectId=" + projectid.ToString() + "\">添加</a></td>");
            sb.Append("<td align='center'><input type=checkbox name='chk_' value ='" + SysFun.ToTrim(dr["id1"]) + "' /></td>");
            sb.Append("<td align='center'>");
            sb.Append("<table>");
            //_strSql = " select id, AccountId_TB, AccountId_SC, CompanyName_SC, ProjectId from project_bid_company where accountid_tb=" + SysFun.ToTrim(dr["AccountId_TB"]) + " and projectid=" + projectid.ToString();
            //dt = db2.DataTable(_strSql);
            //for(int i=0 ;i<dt.Rows.Count;i++)
            //{
            //    string _strCompanyName_SC = SysFun.ToTrim(dt.Rows[i][3]);
            //    if (SysFun.ToTrim(_strCompanyName_SC) != "")
            //    {
            //        sb.Append("<tr><td>" + _strCompanyName_SC + "</td><td><input type=checkbox name='chkid2' value ='" + SysFun.ToTrim(dt.Rows[i][0]) + "' /></td></tr>");
            //    }
            //    else
            //    {
            //        sb.Append("<tr><td>&nbsp;</td><td>&nbsp;</td></tr>");
            //    }
                
            //}
            _strSql = " select distinct company.companyname,company.usercode from project_bid inner join goods on project_bid.goodsid=goods.goodsid inner join company on goods.AccountId_SC=Company.AccountId where project_bid.accountid=" + SysFun.ToTrim(dr["AccountId_TB"]) + " and project_bid.projectid=" + projectid.ToString() + " order by company.usercode";
            dt = db2.DataTable(_strSql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string _strCompanyName_SC = SysFun.ToTrim(dt.Rows[i][0]);
                string _strUserCode_SC = SysFun.ToTrim(dt.Rows[i][1]);

                if (SysFun.ToTrim(_strCompanyName_SC) != "")
                {
                    sb.Append("<tr><td><strong>" + (i + 1).ToString() + "</strong></td><td>" + _strUserCode_SC + "</td><td>" + _strCompanyName_SC + "</td></tr>");
                }
                else
                {
                    sb.Append("<tr><td>&nbsp;</td></tr>");
                }

            }
            sb.Append("</table>");
            sb.Append("</td></tr>");
            dt.Dispose();
            //}
            //else
            //{
            //    sb.Append("<tr><td>" + SysFun.ToTrim(dr["CompanyName_SC"]) + "</td><td><input type=checkbox name='chkid2' value ='" + SysFun.ToTrim(dr["id2"]) + "' /></td></tr>");
            //}
            pcaID.Append(SysFun.ToTrim(dr["id1"]) + ",");
        }
        db2.Close();
        string strpcaID = pcaID.ToString();
        if (strpcaID.Length != 0)
        {
            strpcaID = strpcaID.Substring(0, strpcaID.Length - 1);
        }
        this.ViewState["PCAID"] = strpcaID;

        sb.Append("</table>");
        this.DbPageFlip1.DbPage = dbPage;

        Label1.Text = sb.ToString();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();

        db.Close();



    }
    protected void Button2_Click(object sender, EventArgs e) //删除
    {

        string keyid = "";
        string sql = "";
        if ((Request.Form["chk_"] != null) || Request.Form["chkid2"] != null)
        {
            Db db = new Db();
            keyid = SysFun.ToTrim(Request.Form["chk_"]);
            if (keyid != "")
            {
                sql = "delete project_bid_company where projectid=" + projectid.ToString() + " and AccountId_TB in (select accountid from ProjectCompanyAttribution where id  in (" + keyid + "))";
                db.Command(sql);
                sql = "delete ProjectCompanyAttribution where id in (" + keyid + ")";
                db.Command(sql);
            }
            keyid = SysFun.ToTrim(Request.Form["chkid2"]);
            if (keyid != "")
            {
                sql = "delete project_bid_company where id in (" + keyid + ")";
                db.Command(sql);
            }
            db.Close();
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/Company_manage.aspx");

            Button2.Dispose();
            Init1(projectid);
        }
        else
        {
            Response.Write("<script language='javascript'>alert('请选择企业！');</script>");
        }
    }

}
